package com.longsunhd.yum.buffalo.base;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.longsunhd.yum.buffalo.eventbus.BaseEvent;
import com.longsunhd.yum.buffalo.util.StatusBarUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;

/**
 * 作者：黄宣淇
 * 时间：2021/5/7 0007
 * 如何代码没有备注，一个月后，只有你和上帝知道--鲁迅
 */
public abstract class BaseActivity extends AppCompatActivity {
    protected Activity mActivity;
    protected void initWindow() {
        //沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        }
    }
    protected abstract int getContentView();

    protected void initWidget() {
    }

    protected void initData() {
    }
    protected boolean initBundle(Bundle bundle) {
        return true;
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(BaseEvent baseEvent) {

    }
    public boolean useEventBus() {
        return false;
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (initBundle(getIntent().getExtras())) {

            setContentView(getContentView());
            if(useEventBus()){
                //注册 Eventbus
                EventBus.getDefault().register(this);
            }
            ButterKnife.bind(this);
            mActivity=this;

            initWindow();
            initWidget();
            initData();
        }else {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(useEventBus()){
            //解除注册 Eventbus
            EventBus.getDefault().unregister(this);
        }
    }
}
